<%--
  Created by IntelliJ IDEA.
  User: 高万檬
  Date: 2020/8/7
  Time: 19:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>学生考勤</title>
    <link rel="stylesheet" href="<%=basePath%>static/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="<%=basePath%>static/css/bootstrap.min.css">
    <script type="text/javascript" src="<%=basePath%>static/js/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/jqPaginator.js"></script>
</head>
<body>
选择课程 &nbsp班级
<div id="showcourse"></div>

<button id="btn" class="btn btn-default" role="button"  onclick="showStudent()">查询</button>


<div id="window1"
     style="position: absolute;left: 30%;top: 20%;width:500px;height: 500px; background-color: floralwhite;z-index:999;display: none">
    <div style="margin: 20px 10px ">

        <input type="hidden" id="score_id">
        <span>学号:</span><input class="form-control" type="text"  id="sno" disabled><br>
        <span>姓名:</span><input class="form-control" type="text"  id="sname" disabled><br>
        <span>课程id:</span><input class="form-control" type="text"  id="coursename" disabled><br>
        <span>考试成绩:</span><input class="form-control" type="text"  id="check_score"><br>

        <center style="margin-top: 25px">
            <input class="btn btn-default" style="background-color:steelblue;color:white;display: inline-block;margin: 0 auto"
                   type="button" value="添加" onclick="addCourse()">
            <input class="btn btn-default" style="background-color:steelblue;color:white;display: inline-block;margin: 0 auto"
                   type="button" value="关闭" onclick="close1()">
        </center>

    </div>
</div>




<div id="window2"
     style="position: absolute;left: 30%;top: 20%;width:500px;height: 500px; background-color: floralwhite;z-index:999;display: none">
    <div style="margin: 20px 10px ">

        <span>学号:</span><input class="form-control" type="text"  id="sno1" disabled><br>
        <span>姓名:</span><input class="form-control" type="text"  id="sname1" disabled><br>
        <span>课程id:</span><input class="form-control" type="text"  id="coursename1" disabled><br>
        <center style="margin-top: 25px">
            <input class="btn btn-default" style="background-color:steelblue;color:white;display: inline-block;margin: 0 auto"
                   type="button" value="未到" onclick="addCheckCourse()">
            <input class="btn btn-default" style="background-color:steelblue;color:white;display: inline-block;margin: 0 auto"
                   type="button" value="关闭" onclick="close2()">
        </center>

    </div>
</div>

<table class="table table-hover">
    <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>班级</th>
        <th>入学年份</th>
        <th>考勤</th>
        <th>考试成绩</th>
    </tr>
    <tbody id="tab"></tbody>
    <tr>
        <td colspan="8">
            <ul class="pagination" id="pagination1"></ul>
        </td>
    </tr>
</table>
<script>
    var total = 0;//总条数
    var totalPages = 0;//总页数
    var visiblePages = 5;//显示的页码数
    var currentPage = 0;//当前页码
    $(document).ready(function () {


        var roleId = <%=session.getAttribute("userRoleId")%>;
        var tid = <%=session.getAttribute("userId")%>;
        console.log(roleId);
        console.log(tid);
        //文档加载完毕就在div中下拉框
        var str = "<select id='course'><option selected='selected'>请选择--</option></select>" +
            "<select id='class'><option selected='selected'>请选择--</option></select>";
        $("#showcourse").html(str);
        $.ajax({
            url: "<%=basePath%>teacher/queryTeacherLession",
            data: {"tid": tid, "roleid": roleId},
            dataType: "json",
            type: "POST",
            success: function (data) {
                console.log(data);
                console.log(data[0].lc_course_id + "5555555555555555");
                for (var i = 0; i < data.length; i++) {

                    //<option value=""></option>
                    var str1 = "<option value='" + data[i].lc_course_id + "'>" + data[i].coursename + "</option>";
                    $("#course").append(str1);
                }

                // $.each(data, function (n,val) {
                //     var str = "";
                //     str = "<option value='"+val.coursename+"'>"+val.coursename+"</option>";
                //     $("#course").append(str);
                // });


            },
            error: function () {
                alert("服务器出错了！")
            }
        });
        //给省下拉框绑定改变事件
        $("#course").change(function () {
            showClass();
        });


    });

    function showClass() {


        var roleId = <%=session.getAttribute("userRoleId")%>;
        var tid = <%=session.getAttribute("userId")%>;
        $("#class").empty();
        var str2 = "<option selected='selected'>请选择--</option>";
        $("#class").append(str2);
        var courseid = $('#course option:selected').val();
        console.log(roleId);
        console.log(tid);
        console.log("showClass收到消息准备发送" + courseid);
        $.ajax({
            url: "<%=basePath%>teacher/queryTeacherClass",
            data: {"courseid": courseid, "roleid": roleId, "tid": tid},
            dataType: "json",
            type: "POST",
            success: function (data) {
                console.log("class信息：" + data);
                console.log(data[0].cname)
                for (var i = 0; i < data.length; i++) {
                    //<option value=""></option>
                    var str3 = "<option value='" + data[i].cname + "'>" + data[i].cname + "</option>";
                    $("#class").append(str3);
                }
            },
            error: function () {
                alert("服务器出错了！")
            }
        });
    }


    function showStudent() {
        showPage(-1);
        $('#pagination1').jqPaginator({
            totalPages: totalPages,
            visiblePages: visiblePages,
            currentPage: currentPage,
            totalCounts: total,
            first: '<li class="first"><a href="javascript:void(0);">第一页</a></li>',
            prev: '<li class="prev"><a href="javascript:void(0);">前一页</a></li>',
            next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
            last: '<li class="last"><a href="javascript:void(0);">最后一页</a></li>',
            page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
            onPageChange: function (num) {
                /* num代表改变过后的页码 */
                showPage(num);
                console.log(num)
            }

        });
    }


    function showPage(n) {
        var cname = $('#class option:selected').val();
        console.log(n)
        console.log(cname);
        $.ajax({
            url: "<%=basePath%>teacher/queryStudent",
            async: false,//同步请求（默认）
            type: "POST",
            dataType: "json",
            data: {"pageNum": n, "pageSize": 4, "cname": cname},
            success: function (data) {
                console.log(data);
                if (n == -1) {/*理解为第一次访问 ，初始化全局变量 */
                    total = data.total;
                    totalPages = data.pages;
                    currentPage = data.pageNum;
                }
                $("#tab").html("");
                if (data.list.length > 0) {
                    //使用jquery形式遍历
                    $.each(data.list, function (n, val) {
                        var str = "";
                        str += "<tr>" +
                            "<td>" + val.sno + "</td>" +
                            "<td>" + val.sname + "</td>" +
                            "<td>" + val.sex + "</td>" +
                            "<td>" + val.cname + "</td>" +
                            "<td>" + val.entrancetime + "</td>" +
                            "<td><a class=\"btn btn-default\" role=\"button\" onclick='checkScore(this)'>未到</a></td>" +
                            "<td><a class=\"btn btn-default\" role=\"button\" onclick='addScore(this)'>录入成绩</a></td>" +
                            "</tr>";
                        $("#tab").append(str);
                    });
                } else {
                    $("#tab").append("<tr><td colspan=8 align=\"center\">暂时没有数据哦，快去添加一条吧</td></tr>");
                }
            },
            error: function () {
                alert("服务器出错！");
            }
        });
    }


    //教师考勤学生
    function addCheckCourse() {
        var courseid = $('#course option:selected').val();
        var sno=$("#sno1").val();
        console.log("我开始考勤学生了"+courseid+"****"+sno);
        $.ajax({
            url: "<%=basePath%>teacher/teacherCheckStudent",
            async: false,//同步请求（默认）
            type: "POST",
            dataType: "json",
            data: {"sno":sno,
                "courseid":courseid},
            success: function (data) {
                if (data==1){
                    alert("添加成功！");
                }else {
                    alert("添加失败！");
                }
                // alert(data[0].info);
                //关闭模态框
                $("#window2").hide();
                // location.reload();
            }
        });
    }

    function addScore(obj){
        $("#window1").show();
        //获取所有数据

        var sno = $(obj).parent().parent().parent().find("td").eq(0).text();
        var sname = $(obj).parent().parent().parent().find("td").eq(1).text();
        var coursename = $('#course option:selected').val();
        var check_score = $(obj).parent().parent().parent().find("td").eq(6).text();
        //把选中行的数据复制给模态框里的表单元素
        $("#sno").val(sno);
        $("#sname").val(sname);
        $("#coursename").val(coursename);
        $("#check_score").val(check_score);
    }


    //学生考勤
    function checkScore(obj){
        //获取所有数据
        $("#window2").show();
        var sno = $(obj).parent().parent().parent().find("td").eq(0).text();
        var sname = $(obj).parent().parent().parent().find("td").eq(1).text();
        var coursename = $('#course option:selected').val();

        //把选中行的数据复制给模态框里的表单元素
        $("#sno1").val(sno);
        $("#sname1").val(sname);
        $("#coursename1").val(coursename);

    }


    function addCourse() {
        var sno=$("#sno").val();
        var couresid=$('#course option:selected').val()
        console.log("前端cid"+couresid)
        var sname = $("#sname").val();
        var check_score = $("#check_score").val();
        var flag = confirm("你确定添加 "+sname+"，考试成绩为:"+check_score+" 吗？");
        if (flag){
            $.ajax({
                url: "<%=basePath%>/teacher/teacherAddScore",
                type: "POST",
                data: {
                    "sno":sno,
                    "couresid":couresid,
                    "check_score":check_score
                },
                success: function (data) {
                    console.log(data)
                    if (data==1){
                        alert("添加成功！");
                    }else {
                        alert("添加失败！");
                    }
                    // alert(data[0].info);
                    //关闭模态框
                    $("#window1").hide();
                    // location.reload();
                }
            })
        }else {
            //关闭模态框
            $("#window1").hide();
        }

    }
    function close1() {
        $("#window1").hide();
    }
    function close2() {
        $("#window2").hide()
    }


</script>


</body>
</html>
